package com.example.spring.demo.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.spring.demo.common.error.CustomException;
import com.example.spring.demo.common.model.vo.MakeMenuTree;
import com.example.spring.demo.common.model.vo.PermissionVo;
import com.example.spring.demo.entity.SysMenu;
import com.example.spring.demo.entity.SysUser;
import com.example.spring.demo.service.SysMenuService;
import com.example.spring.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysUserService sysUserService;
    //新增
    @PostMapping
    public String add(@RequestBody SysMenu sysMenu){
        sysMenu.setCreateTime(new Date());
        if(sysMenuService.save(sysMenu)){
            return "新增成功!";
        }
        throw new CustomException("新增失败!");
    }

    //编辑
    @PutMapping
    public String edit(@RequestBody SysMenu sysMenu){
        if(sysMenuService.updateById(sysMenu)){
            return "编辑成功!";
        }
        throw new CustomException("编辑失败!");
    }

    //删除
    @DeleteMapping("/{menuId}")
    public String delete(@PathVariable("menuId") Long menuId){
        if(sysMenuService.removeById(menuId)){
            return "删除成功!";
        }
        throw new CustomException("删除失败!");
    }

    //列表
    @GetMapping("/list")
    public List<SysMenu> list(){
        QueryWrapper<SysMenu> query = new QueryWrapper<>();
        query.lambda().orderByAsc(SysMenu::getOrderNum);
        List<SysMenu> menuList = sysMenuService.list(query);
        //组装树形数据
        return MakeMenuTree.makeTree(menuList, 0L);
    }

    //上级菜单
    @GetMapping("/getParent")
    public List<SysMenu> getParent(){
        return sysMenuService.getParent();
    }
    //分配菜单树数据查询和回显
    @GetMapping("/getAssignTree")
    public PermissionVo getAssignTree(Long userId, Long assId){
        //查询当前用户的信息
        SysUser user = sysUserService.getById(userId);
        //查询菜单信息
        List<SysMenu> menuList = null;
        //判断用户是否是超级管理员，超级管理员拥有所有的权限
        if(StrUtil.isNotEmpty(user.getIsAdmin()) && "1".equals(user.getIsAdmin())){
            menuList = sysMenuService.list();
        }else{
            //根据用户id查询
            menuList = sysMenuService.getMenuByUserId(userId);
        }
        //组装树数据
        List<SysMenu> menus = MakeMenuTree.makeTree(menuList, 0L);
        //设置菜单数据
        PermissionVo vo = new PermissionVo();
        //设置菜单数据
        vo.setMenuList(menus);
        //查询回显的数据
        List<SysMenu> menuByUserId = sysMenuService.getMenuByUserId(assId);
        List<Long> ids = new ArrayList<>();
        Optional.ofNullable(menuByUserId).orElse(new ArrayList<>())
                .stream()
                .filter(Objects::nonNull)//过滤空值
                .forEach(item ->{
                    ids.add(item.getMenuId());//获取菜单id
                });
        //设置回显的数据
        vo.setCheckList(ids.toArray());
        return vo;
    }
}
